<?php
namespace app\facility\util;
require_once(ROOT_PATH . 'extend/CCP_REST/SDK/CCPRestSDK.php');


class CCPHelper
{
    /**
     * 创建子帐号
     * @param friendlyName 子帐号名称
     */
    public function createSubAccount($friendlyName) {
        $accountSid = config('ccp_accountSid');
        $accountToken = config('ccp_accountToken');
        $appId = config('ccp_appId');
        $serverIP = config('ccp_serverIP');
        $serverPort = config('ccp_serverPort');
        $softVersion = config('ccp_softVersion');
        // 初始化REST SDK
        $rest = new \REST($serverIP,$serverPort,$softVersion);
        $rest->setAccount($accountSid,$accountToken);
        $rest->setAppId($appId);
        
        // 调用云通讯平台的创建子帐号,绑定您的子帐号名称
        $result = $rest->CreateSubAccount($friendlyName);
        if ($result == NULL ) {
            return false;
            // echo "result error!";
            // break;
        }
        if ($result->statusCode!=0) {
            return $result;
            //TODO 添加错误处理逻辑
        } else {
            // 获取返回信息
            $subaccount = $result->SubAccount;
            $subaccount->friendlyName = $friendlyName;
            
            return $subaccount;
        }      
    }

    /**
      * 获取子帐号
      * @param startNo 开始的序号，默认从0开始
      * @param offset 一次查询的最大条数，最小是1条，最大是100条
      */
    public function getSubAccounts($startNo,$offset)
    {
        $accountSid = config('ccp_accountSid');
        $accountToken = config('ccp_accountToken');
        $appId = config('ccp_appId');
        $serverIP = config('ccp_serverIP');
        $serverPort = config('ccp_serverPort');
        $softVersion = config('ccp_softVersion');
        // 初始化REST SDK
        $rest = new \REST($serverIP,$serverPort,$softVersion);
        $rest->setAccount($accountSid,$accountToken);
        $rest->setAppId($appId);
        
        // 调用云通讯平台的获取子帐号接口
        $result = $rest->getSubAccounts($startNo,$offset);
        if($result == NULL ) {
            return false;
            // echo "result error!";
            // break;
        }
        if($result->statusCode!=0) {
            return $result;
            //TODO 添加错误处理逻辑
        }else {
            // echo "get SubbAccount list success<br/>";
            // 获取返回信息
            $subaccount = $result->SubAccount;
            for($i =0;$i<count($subaccount);$i++){
                $return[] = $subaccount[$i];
            }
            return $return;
            //TODO 把云平台子帐号信息存储在您的服务器上.
            //TODO 添加成功处理逻辑 
        }          
    }

    public function pushMsg($receiver, $extOpts = '', $msgContent = "", $msgDomain = "")
    {
        $serverIP = config('ccp_serverIP');
        $serverPort = config('ccp_serverPort');
        $softVersion = config('ccp_softVersion');
        $appId = config('ccp_appId');
        // 初始化REST SDK
        $rest = new \REST($serverIP,$serverPort,$softVersion);

        $accountSid= config('ccp_accountSid');
        $authToken= config('ccp_accountToken');
        $timeStamp = date('YmdHis');
        $https = "https://".config('ccp_serverIP').':'.config('ccp_serverPort').'/'.config('ccp_softVersion');

        $SigParameter = md5($accountSid.$authToken.$timeStamp);
        $Authorization = base64_encode($accountSid.':'.$timeStamp);

        // 生成包头 
        $header = array("Accept:application/json","Content-Type:application/json;charset=utf-8","Authorization:{$Authorization}");
        $url = $https."/Accounts/{$accountSid}/IM/PushMsg?sig={$SigParameter}";

        $extOpts = isset($extOpts) ? base64_encode(json_encode($extOpts)) : '';
        $msgDomain = isset($msgDomain) ? json_encode($msgDomain) : '';
        $data = [
            'pushType'  =>  '1', //1：个人，2：群组，默认为1
            'appId'  =>  $appId,
            'sender'  =>  '8021579500000026',
            'receiver'  =>  $receiver,
            'msgType'  =>  '1',
            'msgContent'  =>  $msgContent,
            'msgDomain'  =>  $msgDomain,
            'msgFileName'  =>  '',
            'msgFileUrl'  =>  '',
            'extOpts'  =>  $extOpts,
        ];
        $data = json_encode($data);
        // 发请求
        $result = $rest->curl_post($url,$data,$header);
        return json_decode($result, true);
    }


}